package com.atguigu.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.product.entity.ProductAttrValueEntity;
import com.atguigu.product.vo.AttrRespVo;
import com.atguigu.product.vo.AttrVo;
import com.atguigu.product.service.AttrService;
import org.springframework.web.bind.annotation.*;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author polarisink
 * @email polarisink@163.com
 * @date 2020-12-18 14:19:34
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
  @Resource
  private AttrService attrService;


  ///product/attr/base/list/{catelogId}
  @RequestMapping("/{attrType}/list/{catelogId}")
  public R baseAttrList(@RequestParam Map<String, Object> params,
                        @PathVariable("catelogId") Long catelogId,
                        @PathVariable("attrType") String attrType) {

    PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
    return R.ok().put("page", page);
  }

  @GetMapping("/base/listforspu/{spuId}")
  public R listAttrsforSpu(@PathVariable("spuId") Long spuId) {
    List<ProductAttrValueEntity> productAttrValueEntities = attrService.listAttrsforSpu(spuId);
    return R.ok().put("data", productAttrValueEntities);
  }

  /**
   * 列表
   */
  @RequestMapping("/list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = attrService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{attrId}")
  public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
    AttrRespVo respVo = attrService.getAttrInfo(attrId);
    return R.ok().put("attr", respVo);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody AttrVo attr) {
    attrService.saveAttr(attr);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody AttrVo attr) {
    attrService.updateAttr(attr);

    return R.ok();
  }

  @PostMapping("/update/{spuId}")
  public R updateSpuAttrs(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> attrValueEntities) {
    attrService.updateSpuAttrs(spuId, attrValueEntities);
    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] attrIds) {
    attrService.removeByIds(Arrays.asList(attrIds));

    return R.ok();
  }

}
